﻿/*************************************************************************
 *
 * File: flyout.js
 *
 * Description: 
 *
 ************************************************************************/

var rdp_settings = {'servers' : []}
var builder = new GadgetBuilder();
var filesystem = null;
			
function Init()
{
	try
	{
		builder.Initialize();
		filesystem = builder.LoadType(System.Gadget.path + "\\bin\\RDPUtils.dll", "Utils.FileSystem");
		
		var rdp_settings_json = System.Gadget.Settings.readString("rdp_settings_json");
		if (rdp_settings_json)
		{
			rdp_settings = JSON.parse(rdp_settings_json);
			System.Debug.outputString("flyout.Init:rdp_settings = " + rdp_settings);
			System.Debug.outputString("flyout.Init:rdp_settings.servers.length = " + rdp_settings.servers.length);	
			System.Debug.outputString("flyout.Init:rdp_settings.servers[0].displayName = " + rdp_settings.servers[0].displayName);
		}	
		else
		{	
			rdp_settings_json = filesystem.ReadFile(System.Gadget.path + "\\settings.txt");			
			System.Debug.outputString("flyout.Init: rdp_settings_json = " + rdp_settings_json);
		   
			if (rdp_settings_json)
			{			
				rdp_settings = JSON.parse(rdp_settings_json);
				PopulateServerTree();			
			}		
		}
		
		PopulateServerTree();
		
		$(document).ready(function() {
			$("#menu li ul").hide(); 
			$("#menu li").hover(
				function () {
				$(this).children("ul").show();
				},function(){
				$(this).children("ul").hide();
			});//hover
		});// document ready
	}
	catch(e)
    {
        System.Debug.outputString("ERROR:flyout.Init: " + e.description);
    }
} 
 
function PopulateServerTree()
{	
	try
	{
		var categories = new Array();
		var prevCategory = "";
		for (var index = 0; index < rdp_settings.servers.length; index++)
		{
			var server = rdp_settings.servers[index];
			if (prevCategory != server.category) 
			{
				categories.push(server.category);
				prevCategory = server.category;
			}
		}
		
		var html = "<ul id='menu'>";
		
		for (var index = 0; index < categories.length; index++)
		{
			var category = categories[index];
			html += "<li class='menu'>" + category 
				html += "<ul>";
				for (var index2 = 0; index2 < rdp_settings.servers.length; index2++)
				{
					var server = rdp_settings.servers[index2];
					if (server.category == category)
					{
						html += "<li><a href='#' onclick='return LaunchServer(" + index2 + ");'>" + server.displayName + "</a></li>";
					}
				}	
				html += "</ul>";
			html += "</li>";		
		}
		html += "</ul>";
		divServerTree.innerHTML = html;
	}
	catch(e)
    {
        System.Debug.outputString("ERROR:flyout.PopulateServerTree: " + e.description);
    }
} 
 
function HideFlyout()
{
	try
	{		
		System.Gadget.Flyout.show = false;		
	}
    catch(e)
    {
        System.Debug.outputString("ERROR:flyout.HideFlyout: " + e.description);
    }
}

function LaunchServer(index)
{
	try
	{		
		System.Debug.outputString("flyout.LaunchServer: index = " + index);						
		var server = rdp_settings.servers[index].serverName;
		var encryption = builder.LoadType(System.Gadget.path + "\\bin\\Utils.dll", "Utils.Encryption");
		var password = encryption.encrypt("password");
		var rdp_config = "screen mode id:i:2\n";
			rdp_config += "desktopwidth:i:1024\n";
			rdp_config += "desktopheight:i:768\n";
			rdp_config += "session bpp:i:16\n";
			rdp_config += "full address:s:" + server + "\n";
			rdp_config += "compression:i:1\n";
			rdp_config += "keyboardhook:i:1\n";
			rdp_config += "audiomode:i:2\n";
			rdp_config += "redirectdrives:i:0\n";
			rdp_config += "redirectprinters:i:0\n";
			rdp_config += "redirectcomports:i:0\n";
			rdp_config += "redirectsmartcards:i:0\n";
			rdp_config += "displayconnectionbar:i:1\n";
			rdp_config += "autoreconnectionenabled:i:1\n";
			rdp_config += "username:s:jorge.matos\n";
			rdp_config += "domain:s:CORP\n";
			rdp_config += "disable wallpaper:i:1\n";
			rdp_config += "disable full window drags:i:1\n";
			rdp_config += "disable menu anims:i:1\n";
			rdp_config += "disable themes:i:1\n";
			rdp_config += "disable cursor setting:i:1\n";
			rdp_config += "bitmapcachepersistenable:i:1\n";
			rdp_config += "password 51:b:" + password + "\n";

		var filename = System.Gadget.path + "\\" + server + ".rdp";
		
		filesystem.SaveToFile(rdp_config, System.Gadget.path + "\\" + server + ".rdp");
		//System.Shell.execute(filename);
		return false;
	}
    catch(e)
    {
        System.Debug.outputString("ERROR:flyout.LaunchServer: " + e.description);
    }
}